#include <bits/stdc++.h>

using namespace std;

const int MAXN = 2e5 + 10;
const int MAXA = 2e3 + 10;
long long a[MAXN];
long long dp[MAXA][MAXA]; 
long long maxl[MAXA];
int hs[MAXA];
int n, t;

void solve() {
    long long maxa = 0, ans = 0;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        maxa = max(a[i], maxa);
    }
    for (int i = 0; i <= maxa; ++i) {
        maxl[i] = 0;
        hs[i] = 0;
        for (int j = 0; j <= maxa; ++j) {
            dp[i][j] = 0;
        }
    }
    hs[0] = 1;
    hs[a[1]]++;
    hs[a[2]]++;
    if (a[1] == a[2]) {
        dp[a[1]][a[2]] = a[1];
        maxl[a[1]] = a[1];
    }
    for (int i = 3; i <= n; ++i) {
        for (int j = 0; j <= maxa; ++j) {
            if (!hs[j]) continue;
            if (j != a[i - 1]) {
                dp[a[i]][j] = dp[a[i - 1]][j];
                if (a[i] == a[i - 1]) dp[a[i]][j] += a[i];
            }
            else {
                if ((a[i] == j ? hs[j] > 1 : hs[a[i]])) dp[a[i]][j] = max(dp[a[i]][j] + a[i], maxl[j]);
                else dp[a[i]][j] = maxl[j];
            }
            maxl[j] = max(maxl[j], dp[a[i]][j]);
            maxl[a[i]] = max(maxl[a[i]], dp[a[i]][j]);
            dp[j][a[i]] = dp[a[i]][j];
        }
        hs[a[i]]++;
    }
    for (int i = 0; i <= maxa; ++i) {
        for (int j = 0; j <= maxa; ++j) ans = max(ans, dp[i][j]);
    }
    //cout << dp[2][4] << endl;
    cout << ans << "\n";
}

int main() {
    cin.tie(0), cout.tie(0), ios::sync_with_stdio(false);
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    cin >> t;
    while(t--) solve();
    return 0;
}
